package com.ft.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.ResponseWrapper;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ft.entity.Student;
import com.ft.utils.ExcelUtil;

/**
 * 
 * @author fengtao
 * @CreateTime 2016年7月29日下午4:34:21
 * @Description TODO
 */
@RequestMapping(value="/excel")
@Controller
public class ExcelController {
	
	@RequestMapping(value="/download")
	public void downloadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception{
		System.out.println("确定进来的");
		List<Student> list = new ArrayList<Student>();
		for(int i=0;i<100;i++){
			Student student = new Student();
			student.setName("xuesheng" + i);
			list.add(student);
		}
		try {
			OutputStream outputStream = response.getOutputStream();  
			//得到想客服端输出的输出流  
	        //如果下载名是中文，则文件名需要经过url编码URLEncoder.encode(filename,"UTF-8")(平常直接+文件名即可)  
//	        response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8")); 
			
			
			//客服端使用保存文件的对话框  
			response.setHeader("Content-disposition", "attachment;filename="+"asd.xlsx");  
			//通知客服文件的MIME类型  
			response.setContentType("application/octet-stream");  
			//通知客服文件的长度  
			ExcelUtil excelUtil = new ExcelUtil();
//			Workbook workbook = ExcelUtil.createWorkbook(ExcelUtil.VERSION_2007, "axiba");
			List<String> list1 = new ArrayList<String>();
			list1.add("姓名");
			list1.add("年龄");
			list1.add("性别");
			List<String> list2 = new ArrayList<String>();
			list2.add("name");
			list2.add("age");
			list2.add("sex");
			Workbook workbook = excelUtil.outputExcelResult(0, "axiba", list1, list2, "大的阿西吧", list, Student.class, ExcelUtil.VERSION_2007);
			workbook.write(outputStream);
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
